<?php

namespace common\models;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;
use common\helpers\IpHelper;
use common\models\mongo\SystemLog;
use yii\helpers\Json;

/**
 * Description of AdminLog
 *
 * @author emhome
 */
class AdminLog {

    public static function afterFind($event) {
        // 具体要记录什么东西，自己来优化$description
        if (!empty($event->changedAttributes)) {
            $desc = '';
            foreach ($event->changedAttributes as $name => $value) {
                $desc .= $name . ' : ' . $value . '=>' . $event->sender->getAttribute($name) . ',';
            }
            $desc = substr($desc, 0, -1);
            $description = Yii::$app->user->identity->username . '修改了' . $event->sender->className() . 'id:' . $event->sender->primaryKey()[0] . '的' . $desc;
            $route = Url::to();
            $userId = Yii::$app->user->id;
            $model = new SystemLog();
            $model->setAttributes([
                'route' => $route,
                'description' => $description,
                'user_id' => $userId
            ]);
            $model->save();
        }
    }

    /**
     * 插入操作.
     * @param obj $event
     * @return mixed
     */
    public static function afterInsert($event) {
        $sender = $event->sender;
        if ($sender instanceof \yii\db\ActiveRecord) {
            $description = '';
            $attributes = $sender->attributes;
            ArrayHelper::remove($attributes, 'gis');
            self::log([
                'table' => $sender::tableName(),
                'class' => $sender::className(),
                'type' => SystemLog::TYPE_INSERT,
                'description' => $description,
                'data' => $attributes,
            ]);
        }
    }

    /**
     * 修改操作
     * @param obj $event
     * @return mixed
     */
    public static function afterUpdate($event) {
        $sender = $event->sender;
        if ($sender instanceof \yii\db\ActiveRecord) {
            if (empty($event->changedAttributes)) {
                return;
            }
            $description = '';
            $attributes = $sender->attributes;
            ArrayHelper::remove($attributes, 'gis');
            $data = [
                'old' => $event->changedAttributes,
                'new' => $attributes,
            ];
            self::log([
                'table' => $sender::tableName(),
                'class' => $sender::className(),
                'type' => SystemLog::TYPE_UPDATE,
                'description' => $description,
                'data' => $data,
            ]);
        }
    }

    /**
     * 删除操作.
     * @param obj $event
     * @return mixed
     */
    public static function afterDelete($event) {
        $sender = $event->sender;
        if ($sender instanceof \yii\db\ActiveRecord) {
            $description = '';
            $attributes = $sender->attributes;
            ArrayHelper::remove($attributes, 'gis');
            self::log([
                'table' => $sender::tableName(),
                'class' => $sender::className(),
                'type' => SystemLog::TYPE_DELETE,
                'description' => $description,
                'data' => $attributes,
            ]);
        }
    }

    public static function log($data) {
        $model = new SystemLog();
        $request = Yii::$app->request;
        $headers = $request->getHeaders()->toArray();
        foreach ($headers as $key => $vls) {
            $headers[$key] = $vls[0];
        }
        $requestData = [
            'method' => $request->getMethod(),
            'url' => $request->getUrl(),
            'queryParams' => $request->getQueryParams(),
            'bodyParams' => $request->getBodyParams(),
            'headers' => $headers,
            'userAgent' => $request->getUserAgent(),
        ];
        $model->attributes = [
            'route' => Url::to(),
            'user_id' => Yii::$app->user->id,
            'request' => $requestData,
            'ip' => IpHelper::ip(),
            'site' => Yii::$app->id,
        ];
        $model->setAttributes($data);
        $model->save(false);
    }

}
